import { TypeAdmin } from '@src/interfaces/TypeAdmin.ts'
import ajax from '@util/ajax.ts'
import { Result } from '@src/interfaces/TypeResult.ts'
import { TypePageDTO } from '@src/interfaces/TypePageDTO.ts'

/**
 * 后台管理员登录
 */
export function adminLoginApi(admin: TypeAdmin) {
  return ajax.post('/back-admin/login', admin).then((res) => {
    return res.data as Result<string>
  })
}

/**
 * 后台管理员注册
 */
export function adminRegisterApi(admin: TypeAdmin) {
  return ajax.post('/back-admin/register', admin).then((res) => {
    return res.data as Result<any>
  })
}

/**
 * 获取管理员列表
 */
export function getAdminListApi(params: {
  page: number
  size: number
  name?: string
  account?: string
  phone?: string
}) {
  return ajax.get('/back-admin/list', { params }).then((res) => {
    return res.data as Result<TypePageDTO<TypeAdmin>>
  })
}

/**
 * 批量删除管理员
 */
export function deleteAdminsApi(adminIds: number[]) {
  let data = JSON.stringify(adminIds)
  data = data.replace('[', '')
  data = data.replace(']', '')
  return ajax
    .delete(`/back-admin`, {
      params: {
        backAdminIds: data,
      },
    })
    .then((res) => {
      return res.data as Result<any>
    })
}

/**
 * 更新管理员信息
 */
export function updateAdminApi(admin: TypeAdmin) {
  return ajax.put('/back-admin', admin).then((res) => {
    return res.data as Result<string>
  })
}
